package com.boco.pciv.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.util.StrUtil;
import com.boco.pciv.core.entity.BizTree;
import com.boco.pciv.core.entity.Constants;
import com.boco.pciv.core.entity.CriteriaBase;
import com.boco.pciv.core.service.PcivBaseServiceImpl;
import com.boco.pciv.entity.sys.DataDict;
import com.boco.pciv.entity.sys.DictDetailInfo;
import com.boco.pciv.entity.sys.DictTypeInfo;
import com.boco.pciv.mapper.sys.IDataDictMapper;
import com.boco.pciv.service.sys.IDataDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DataDictServiceImpl extends PcivBaseServiceImpl<IDataDictMapper,DictDetailInfo,Integer> implements IDataDictService {
    Logger log = LoggerFactory.getLogger(DataDictServiceImpl.class);

    @Autowired
    IDataDictService self;
    @Override
    @Cacheable("dict")
	public List<DataDict> getOrgListByType(String ddType) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("DictTypeCode",ddType);
        List<DictDetailInfo> list=  mapper.queryList(map);
        if (list!= null){
            for (DictDetailInfo item:list){
                final String pid = item.getParentKey();
                if (StrUtil.isBlank(pid)){
                    continue;//不处理
                }
                else if(pid.equals(Constants.DataDictParentID)){
                    item.setParentName(Constants.DataDictParentName);
                }
                else{
                    //查找父节点
                    DictDetailInfo parentDict = CollectionUtil.findOneByField(list, "parentKey", pid);
                    if (parentDict!= null){
                        item.setParentName(parentDict.getDictValue());
                    }
                }
            }
        }

        List<DataDict> dataDicts = new ArrayList<>();
        for (DictDetailInfo info : list) {
            DataDict dataDict = new DataDict();
            BeanUtil.copyProperties(info,dataDict);
            dataDicts.add(dataDict);
        }
        return dataDicts;
    }

    @Override
    public BizTree getDataDictTreeByParent(String ddType, String parentKey) {
        BizTree tree = new BizTree();

        if(StrUtil.isBlank(parentKey) || Constants.DataDictParentID.equals(parentKey)){
            tree.setId(Constants.DataDictParentID);
            tree.setName(Constants.DataDictParentName);
        }
        else{
            DictDetailInfo root = self.getOneByKey(ddType,parentKey);
            if(root!=null) {
                tree.setId(root.getDictKey());
                tree.setName(root.getDictValue());
            }
        }

        List<DataDict> list = self.getOrgListByType(ddType);
        tree.setChildren(AddChildResource(tree,list));

        return tree;
    }
    private  List<BizTree> AddChildResource(final BizTree curNode, List<DataDict> AllSource) {
        Collection<DataDict> childLst = CollectionUtil.filter(AllSource, new Editor<DataDict>() {
            @Override
            public DataDict edit(DataDict info) {
                if (info.getParentKey().equals(curNode.getId())) {
                    return info;
                } else {
                    return null;
                }
            }
        });


        List<BizTree> childResourceTree = new ArrayList<BizTree>();
        if (childLst!= null) {
            for (DataDict item : childLst) {
                BizTree cur = new BizTree();
                cur.setId(item.getDictKey());
                cur.setName(item.getDictValue());
                cur.setChildren(AddChildResource(cur, AllSource));
                childResourceTree.add(cur);
            }
        }
        return childResourceTree;
    }
    @Override
    public List<DataDict> getListByType(String ddType, final String parentKey) {
        List<DataDict> alllist=  getOrgListByType(ddType);
        List<DataDict> list = alllist;
        if (!StrUtil.isBlank(parentKey)){
            //根据父节点筛选
            Collection<DataDict> childLst = CollectionUtil.filter(alllist, new Editor<DataDict>() {
                @Override
                public DataDict edit(DataDict info) {
                    if (info.getParentKey().equals(parentKey)) {
                        return info;
                    } else {
                        return null;
                    }
                }
            });
        }
        //判断是否为父节点
        for (DataDict item:list){
            String cid = item.getDictKey();

            //查找是否存在子节点
            for (DataDict allitem:alllist){
                if(allitem.getParentKey().equals(cid)){
                    item.setIsParent(true);
                    break;
                }
            }
        }

        return  list;
    }

    @Override
    @Cacheable("dict")
    public DictDetailInfo getOneByKey(String dictType, String dictKey) {
        Map<String,String> map = new HashMap<String,String>();
        map.put("dictType",dictType);
        map.put("dictKey",dictKey);
        return mapper.queryOneByKey(map);
    }

    @Override
	public List<DictTypeInfo> getDDTypeList() {
		return mapper.queryDDTypeList();
	}

    @Override
    @CacheEvict(value = "dict",allEntries = true)
    public void add(DictDetailInfo entity) {
        super.add(entity);
    }

    @Override
    @CacheEvict(value = "dict",allEntries = true)
    public Integer edit(DictDetailInfo entity) {
        return super.edit(entity);
    }

    @Override
    @CacheEvict(value = "dict",allEntries = true)
    public void delete(Integer integer) {
        super.delete(integer);
    }

    @Override
    @CacheEvict(value = "dict",allEntries = true)
    public void delete(CriteriaBase cond) {
        super.delete(cond);
    }
}
